﻿using AutoMapper;
using CoreProject.DTO;
using CoreProject.IService;
using CoreProject.Model;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Pipelines.Sockets.Unofficial.Arenas;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace CoreProject.Api.Controllers
{

    //订单管理控制器
    [Route("api/[controller]")]
    [ApiController]
    public class HJQ_orderController : ControllerBase
    {
        private readonly IHJQ_orderService _hjq;

        private readonly IMapper _Imap;

        public HJQ_orderController(IHJQ_orderService hjq, IMapper Imap)
        {
            _hjq = hjq;
          _Imap = Imap;
        }
        // GET: api/<HJQ_orderController>
        [HttpGet]
        public async Task<ResultPageList<HJQ_orderDTo>> Get(int page, int limit, bool IAs, string Username="", string shopname="", int ZFstate=1)
        {
        var data =  await _hjq.getpageOrder<int>(page, limit, IAs, Username, shopname, ZFstate);
            ResultPageList<HJQ_orderDTo> re = new ResultPageList<HJQ_orderDTo>();
            var dd = from bb in data.list select new HJQ_orderDTo()
            {
                OrderId = bb.OrderId,
                UserId = bb.User.UserId,
                PaymentMethod = bb.PaymentMethod,
                TotalAmount = bb.TotalAmount,
                ShippingAddress = bb.ShippingAddress,
                RecipientName = bb.RecipientName,
                RecipientPhone = bb.RecipientPhone,
                OrderStatus = bb.OrderStatus,
                CreatedAt = bb.CreatedAt,
                 
                 OrderTime = bb.OrderTime,
                  UpdatedAt = bb.UpdatedAt,
                    Username=bb.User.UserName,
                    HjqOrderDetails=from b in bb.HjqOrderDetails select new HjqOrderDetailDto(){
                    OrderId =b.OrderId,
                    ProductId =b.ProductId,
                    ProductName=b.ProductName,
                     ProductCategory=b.ProductCategory,
                     Quantity =b.Quantity,
                     DetailId=b.DetailId,
                     ProductSpec = b.ProductSpec,
                     TotalPrice =b.TotalPrice,
                     UnitPrice =b.UnitPrice,
                  } 
        






            }; 
            re.count = data.count;
             re.code=data.count>0?"200":"400";
            re.msg = data.count > 0 ? "ok" : "error";
            re.data = dd.ToList() ;
            return re;
        }

        // GET api/<HJQ_orderController>/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<HJQ_orderController>

        //添加
        [HttpPost]
        public async Task<ResultHandle> Post(HJQ_orderDTo hjqdto)
        {
            ResultHandle re = new ResultHandle();
            List<HjqOrder> objlist = await _hjq.GetListAsync(true,a=>a.OrderId,a=>true);
            double dd = (double.Parse(objlist.LastOrDefault().OrderId.Substring(2, 12)))+1;
            hjqdto.OrderId= objlist.LastOrDefault().OrderId.Substring(0, 2)+dd.ToString();

            var obj = new HjqOrder()
            {

                OrderId = hjqdto.OrderId,
                CreatedAt = DateTime.Now,
                OrderTime = DateTime.Now,
                OrderStatus = hjqdto.OrderStatus,
                PaymentMethod = hjqdto.PaymentMethod,
                RecipientName = hjqdto.RecipientName,
                RecipientPhone = hjqdto.RecipientPhone,
                ShippingAddress = hjqdto.ShippingAddress,
                TotalAmount = hjqdto.TotalAmount,
                UserId = hjqdto.UserId,
                UpdatedAt = DateTime.Now,
                HjqOrderDetails = (from a in hjqdto.HjqOrderDetails
                                   select new HjqOrderDetail()
                                   {
                                       DetailId = a.DetailId,
                                       OrderId = a.OrderId,
                                       ProductName = a.ProductName,
                                       UnitPrice = a.UnitPrice,
                                       TotalPrice = a.UnitPrice * a.Quantity,
                                       Quantity = a.Quantity,
                                       ProductSpec = a.ProductSpec,
                                       ProductId = a.ProductId,
                                       ProductCategory = a.ProductCategory

                                   }).ToList()

            };
            var bo =await _hjq.AddAsync(obj);
            re.code =bo?"200":"400";
            re.code = bo ? "200" : "400";
            return re;  
        }

        //PUT api/<HJQ_orderController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }


        //删除只需要删除主表子表数据自动删除
        // DELETE api/<HJQ_orderController>/5
        [HttpDelete("{id}")]
        public async Task<ResultHandle> Delete(string id)
        {
            var obj = await _hjq.GetAsync(a => a.OrderId == id);
             var bo=     await  _hjq.DeleteAsync(obj);
            ResultHandle re = new ResultHandle();
          ;
            re.code =bo  ? "200" : "400";
            re.msg = bo  ? "ok" : "error";
            return re;
        }

        //修改订单状态状态 
        [HttpGet("/HJQ")]
        public async Task<ResultHandle> EditState(string id,byte state)
        {
            var obj = await _hjq.GetAsync(a => a.OrderId == id);
            obj.OrderStatus = state;
            var bo = await _hjq.UpdateAsync(obj);
            ResultHandle re = new ResultHandle();
            ;
            re.code = bo ? "200" : "400";
            re.msg = bo ? "ok" : "error";
            return re;
        }
    }
}
